﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class Absensi : Form
    {
        List<int> groupID = new List<int>();
        private int gid;
        private DateTime date;
        private AbsensiEdit absensiedit;

        public Absensi()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(dgvKelas);
        }

        private void Absensi_Load(object sender, EventArgs e)
        {
            DataTable dt = Database.getInstance().getGroups();

            // Load group
            foreach (DataRow row in dt.Rows)
            {
                groupID.Add(int.Parse(row[0].ToString()));
                cmbGrup.Items.Add(row[1].ToString());
            }
        }

        private void btnLihat_Click(object sender, EventArgs e)
        {
            
        }

        public void updateGrid()
        {
            if (cmbGrup.SelectedIndex < 0)
            {
                MessageBox.Show("Mohon pilih salah satu kelas/grup", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            this.gid = groupID[cmbGrup.SelectedIndex];
            this.date = dtpDate.Value;

            // Get session
            string session;
            if(this.date.Month < 7) session = (this.date.Year-1) + " - " + this.date.Year;
            else session = this.date.Year + " - " + (this.date.Year+1);

            string sql = "SELECT s.stu_id AS [No. Induk], e.stu_no AS [No. Urut], s.stu_name AS [Nama Lengkap], s.stu_gender AS [Jenis Kelamin], a.abb_id, a.abb_sick AS [Sakit], a.abb_perm AS [Izin], a.abb_alpha AS [Alpha], a.abb_reason AS [Alasan Absen] " +
                         "FROM Student AS s INNER JOIN " +
                         "Enrollment AS e ON s.stu_id = e.stu_id INNER JOIN " +
                         "[Group] AS g ON e.group_id = g.group_id LEFT OUTER JOIN " +
                         "(SELECT * FROM Absentee WHERE abb_date = '" + this.date.ToShortDateString() + "') AS a ON s.stu_id = a.stu_id " +
                         "WHERE e.group_id = " + this.gid + " AND " +
                         "e.enr_session = '" + session + "' " +
                         "ORDER BY e.stu_no";
            DataTable dt = Database.getInstance().query(sql);

            // 1. Change absent status which is null to present (hadir)
            // 2. Get attendance summary
            int sick = 0, permission = 0, alpha = 0;
            foreach (DataRow row in dt.Rows)
            {
                if (row[4] == DBNull.Value) continue;

                // Sick?
                if (row[5] != DBNull.Value && Convert.ToBoolean(row[5]) == true) sick++;
                
                // Permission?
                if (row[6] != DBNull.Value && Convert.ToBoolean(row[6]) == true) permission++;

                // Alpha?
                if (row[7] != DBNull.Value && Convert.ToBoolean(row[7]) == true) alpha++;
            }
            lblAbsensi.Text = String.Format("Sakit = {0} , Izin = {1} , Alpha = {2}", sick, permission, alpha);
            lblAbsensi.Visible = true;

            // Set visible column
            dgvKelas.Columns.Clear();
            dgvKelas.DataSource = dt;
            dgvKelas.Columns[4].Visible = false; // hide absent id column

            // Add Edit Column
            DataGridViewButtonColumn editColumn = new DataGridViewButtonColumn();
            editColumn.UseColumnTextForButtonValue = true;
            editColumn.Name = "Edit";
            editColumn.Text = "Edit";
            editColumn.HeaderText = "";
            dgvKelas.Columns.Add(editColumn);

            //// Get attendance summary of the class
            //int sick, permission, alpha;
            //sql = "SELECT * FROM Attendance WHERE group_id = " + this.gid + " AND att_date = '" + this.date.ToShortDateString() + "'";
            //DataTable dt1 = Database.getInstance().query(sql);
            //if (dt1.Rows.Count < 1)
            //{
            //    sick = 0; permission = 0; alpha = 0;
            //}
            //else
            //{
            //    sick = (int)dt1.Rows[0][3];
            //    permission = (int)dt1.Rows[0][4];
            //    alpha = (int)dt1.Rows[0][5];
            //}
            //lblAbsensi.Text = String.Format("S = {0}, I = {1}, A = {2}", sick, permission, alpha);
            //lblAbsensi.Visible = true;
        }

        private void dgvKelas_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewColumn col = dgvKelas.Columns[e.ColumnIndex];
            if (col.Name == "Edit")
            {
                if (this.absensiedit == null || this.absensiedit.IsDisposed)
                {
                    DataGridViewRow row = dgvKelas.Rows[e.RowIndex];
                    string stu_id = row.Cells[0].Value.ToString();
                    string stu_name = row.Cells[2].Value.ToString();
                    int abb_id = (row.Cells[4].Value.ToString() == "")? -1 : int.Parse(row.Cells[4].Value.ToString());
                    string reason = row.Cells[8].Value.ToString();
                    DateTime date = this.date;

                    string status;
                    if (row.Cells[5].Value.ToString() != "") status = "Sakit";
                    else if (row.Cells[6].Value.ToString() != "") status = "Izin";
                    else if (row.Cells[7].Value.ToString() != "") status = "Alpha";
                    else status = "Hadir";
                    
                    absensiedit = new AbsensiEdit(this, stu_id, stu_name, date, abb_id, status, reason);
                }

                absensiedit.MdiParent = this.MdiParent;
                absensiedit.Show();
                absensiedit.Activate();
            }
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            updateGrid();
        }

        private void cmbGrup_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateGrid();
        }

    }
}
